<?php
/**
 * 
 * User/IndexAction.class.php (上传图片处理类)
 *
 * @package      	jgsat
 * @author          liuxun QQ:147613338 <admin@jgsat.cn>
 * @copyright     	Copyright (c) 2008-2011  (http://www.jgsat.com)
 * @license         http://www.jgsat.com/license.txt
 * @version        	jgsat企业网站管理系统 v2.1 2012-10-08 jgsat.cn $
 */
if(!defined("JGsat")) exit("Access Denied");
class UploadAction extends BaseAction
{

	

    public function index()
    {
      // echo  1;exit;
		
	
require_once 'JSON.php';

$php_path = dirname(__FILE__) . '/';
$php_url = dirname($_SERVER['PHP_SELF']) . '/';

//文件保存目录路径
$save_path =  './Uploads/';
//文件保存目录URL
$save_url =  './Uploads/';
//定义允许上传的文件扩展名

$ext_arr = array(
	'image' => array('gif', 'jpg', 'jpeg', 'png', 'bmp'),
	'temporary' => array('gif', 'jpg', 'jpeg', 'png', 'bmp'),
	'flash' => array('swf', 'flv'),
	'media' => array('swf', 'flv', 'mp3', 'wav', 'wma', 'wmv', 'mid', 'avi', 'mpg', 'asf', 'rm', 'rmvb'),
	'file' => array('doc', 'docx', 'xls', 'xlsx', 'ppt', 'htm', 'html', 'txt', 'zip', 'rar', 'gz', 'bz2'),
);
//最大文件大小
$max_size = 10098576;

$save_path = realpath($save_path) . '/';

//PHP上传失败
if (!empty($_FILES['imgFile']['error'])) {
	switch($_FILES['imgFile']['error']){
		case '1':
			$error = '超过php.ini允许的大小。';
			break;
		case '2':
			$error = '超过表单允许的大小。';
			break;
		case '3':
			$error = '图片只有部分被上传。';
			break;
		case '4':
			$error = '请选择图片。';
			break;
		case '6':
			$error = '找不到临时目录。';
			break;
		case '7':
			$error = '写文件到硬盘出错。';
			break;
		case '8':
			$error = 'File upload stopped by extension。';
			break;
		case '999':
		default:
			$error = '未知错误。';
	}
	alert($error);
}

//有上传文件时
if (empty($_FILES) === false) {
	//原文件名
	$file_name = $_FILES['imgFile']['name'];
	//服务器上临时文件名
	$tmp_name = $_FILES['imgFile']['tmp_name'];
	//文件大小
	$file_size = $_FILES['imgFile']['size'];
	//检查文件名
	if (!$file_name) {
		$this->alert("请选择文件。");
	}
	//检查目录
	if (@is_dir($save_path) === false) {
		$this->alert("上传目录不存在。");
	}
	//检查目录写权限
	if (@is_writable($save_path) === false) {
		alert("上传目录没有写权限。");
	}
	//检查是否已上传
	if (@is_uploaded_file($tmp_name) === false) {
		$this->alert("上传失败。");
	}
	//检查文件大小
	if ($file_size > $max_size) {
		$this->alert("上传文件大小超过限制。");
	}
	//检查目录名
	$dir_name = 'temporary';
	if (empty($ext_arr[$dir_name])) {
		$this->alert("目录名不正确。");
	}
	//获得文件扩展名
	$temp_arr = explode(".", $file_name);
	$file_ext = array_pop($temp_arr);
	$file_ext = trim($file_ext);
	$file_ext = strtolower($file_ext);
	//检查扩展名
	if (in_array($file_ext, $ext_arr[$dir_name]) === false) {
		$this->alert("上传文件扩展名是不允许的扩展名。\n只允许" . implode(",", $ext_arr[$dir_name]) . "格式。");
	}
	//创建文件夹
	if ($dir_name !== '') {
		$save_path .= $dir_name . "/";
		$save_url .= $dir_name . "/";
		if (!file_exists($save_path)) {
			mkdir($save_path);
		}
	}
	$ymd = date("Ymd");
	$save_path .= $ymd . "/";
	$save_url .= $ymd . "/";
	if (!file_exists($save_path)) {
		mkdir($save_path);
	}
	//新文件名
	$new_file_name = date("YmdHis") . '_' . rand(10000, 99999) . '.' . $file_ext;
	//移动文件
	$file_path = $save_path . $new_file_name;
	if (move_uploaded_file($tmp_name, $file_path) === false) {
		$this->alert("上传文件失败。");
	}




	// imagejpeg ( $tmp_name,$new_file_name);
	@chmod($file_path, 0644);
	$file_url = $save_url . $new_file_name;

	header('Content-type: text/html; charset=UTF-8');
	$json = new Services_JSON();
	echo $json->encode(array('error' => 0, 'url' => $file_url));
	exit;
}
    }

	function alert($msg) {
		header('Content-type: text/html; charset=UTF-8');
		$json = new Services_JSON();
		echo $json->encode(array('error' => 1, 'message' => $msg));
		exit;
	}

	public function avarat(){
		

		$maxSize  = 1024*10240 ;//1M 设置附件上传大小
		$allowExts  = array("gif","jpg","jpeg","png");// 设置附件上传类型

		//include_once("UploadFile.class.php");
		import ( "@.Oauth.avarat.UploadFile" );
		$upload = new UploadFile();// 实例化上传类
		$upload->maxSize = $maxSize;
		$upload->allowExts = $allowExts;
		$upload->savePath =  './Uploads/temporary/';// 设置附件
		$upload->saveRule = time().sprintf('%04s',mt_rand(0,1000));
		if(!$upload->upload()) {// 上传错误提示错误信息
			$errormsg = $upload->getErrorMsg();
			$arr =  array(
					'msg'=>'1111',
					'error'=>$errormsg, //返回错误
					'imgurl'=>'',//返回图片名
			);
			echo json_encode($arr);
			exit;

		}else{// 上传成功 获取上传文件信息
			$info =  $upload->getUploadFileInfo();
			$imgurl = $info[0]['savename'];
		}
		$arr =  array(
				'msg'=>'0000',
				'error'=>'', //返回错误
				'imgurl'=>$imgurl,//返回图片名
		);
		echo json_encode($arr);
		exit;

	}
	public function avarat_img(){
		$img=$_REQUEST['img'];
		//echo ($img);exit;
		if($img==""){
			echo "图片没找到！";
			exit;
		}
		$img='./Uploads/temporary/'.$img;
		$this->assign('img',$img);

		$callback="show_head";//artdilog callback
		$this->assign('callback',$callback);
		$this->display();


	}

	public function avarat_submit(){
		$pic_name=$_REQUEST['pic_name'];
		$x=$_REQUEST['x'];
		$y=$_REQUEST['y'];
		$w=$_REQUEST['w'];
		$h=$_REQUEST['h'];
		$targ_w = $targ_h = 100;
		//include_once("jcrop_image.class.php");
		import ( "@.Oauth.avarat.jcrop_image" );
		$filep="./Uploads/temporary/";
		$crop=new jcrop_image($filep, $pic_name,$x,$y,$w,$h,$targ_w,$targ_h);
		$file=$crop->crop();


	}








}
?>